package com.example.a22310.myapplication.Activity;
//红色的R解决不了
import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;

import com.example.a22310.myapplication.GloableClass.Yuejian;
import com.example.a22310.myapplication.ViewModel.Train.AddNewsViewModel;
import com.example.a22310.myapplication.ViewModel.Train.ConsultViewModel;
import com.example.a22310.myapplication.databinding.ActivityConsultBinding;

import static com.example.a22310.myapplication.GloableClass.Yuejian.CHECK_NEWS_SEND_NUMBER;
import static com.example.a22310.myapplication.ViewModel.Master.MasterDetailViewModel.NEWSFLAG;

public class ConsultActivity extends AppCompatActivity {

    ActivityConsultBinding binding;
    private int clubid;
    private int userid;
    private String clubName;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_consult);
        SharedPreferences sharedPreferences = getSharedPreferences("yuejian'data", this.MODE_PRIVATE);
        userid = sharedPreferences.getInt("userId",-999);
        //由于数据不完全，当前用默认的id=2;
        Intent intent=getIntent();
        Bundle bundle = intent.getExtras();
        if (NEWSFLAG == 0 ){
            clubid = 1;
            clubName = "大师问答";
        }else{
            clubid = bundle.getInt("club_id");
            clubName = bundle.getString("clubName");
        }
        binding.title.setText(clubName);
        new ConsultViewModel(this, binding,clubid);
        new AddNewsViewModel(this,binding,clubid,userid);
        handler.postDelayed(runnable, 100*1);

    }
    private Runnable runnable = new Runnable() {
        public void run() {
            this.update();
            handler.postDelayed(this, 100*1);
        }
        void update() {
            if(1 == CHECK_NEWS_SEND_NUMBER){   //检测数据书否发送出去
                new ConsultViewModel(ConsultActivity.this, binding,clubid);                   //yes则刷新消息页面
                Yuejian.CHECK_NEWS_SEND_NUMBER = 0;    //把标志位重置
            }
        }

    };
}

